package cst.a104ljy.widgettest104ljy;

import android.content.Intent;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private EditText editText;
    private Button textbutton,changebutton,sexbutton,hobbybutton;
    private RadioButton radiobutton1,radiobutton2;
    private ImageView imageview;
    private ImageButton imageButton;
   private RadioGroup rg;
    private CheckBox checkBox1,checkBox2,checkBox3;
    private  String msg=" ";
    private void  init(){
        editText=(EditText)findViewById(R.id.editText);
        textbutton=(Button)findViewById(R.id.textbutton);
        changebutton =(Button)findViewById(R.id.changebutton);
        imageview =(ImageView)findViewById(R.id.imageView);
        imageButton =(ImageButton) findViewById(R.id.imageButton);
        sexbutton=(Button)findViewById(R.id.sexbutton);
        radiobutton1 = (RadioButton)findViewById(R.id.radioButton);
        radiobutton2 = (RadioButton)findViewById(R.id.radioButton2);
        rg=(RadioGroup)findViewById(R.id.RadioGroup);
        checkBox1 =(CheckBox)findViewById(R.id.checkBox3);
        checkBox2 = (CheckBox)findViewById(R.id.checkBox4);
        checkBox3 = (CheckBox)findViewById(R.id.checkBox5);
        hobbybutton = (Button)findViewById(R.id.horrybutton);
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        init();
        textbutton.setOnClickListener(this);
        changebutton.setOnClickListener(this);
        imageButton.setOnClickListener(this);
        sexbutton.setOnClickListener(this);
        hobbybutton.setOnClickListener(this);
        rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int i) {
                switch (i){
                    case R.id.radioButton: editText.setText("男");break;
                    case R.id.radioButton2: editText.setText("女");break;
                }
            }
        });
       checkBox1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)msg+="篮球 ";
                else
                    msg=msg.replace("篮球 ","");
                editText.setText(msg);
            }
        });
        checkBox2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)msg+="足球 ";
                else
                    msg=msg.replace("足球 ","");
                editText.setText(msg);
            }
        });
        checkBox3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked)msg+="乒乓球 ";
                else
                    msg=msg.replace("乒乓球 ","");
                editText.setText(msg);
            }
        });
    }

    @Override
    public void onClick(View v) {
       switch(v.getId()){
           case R.id.textbutton:
               Toast.makeText(this, "内容是"+editText.getText().toString(), Toast.LENGTH_SHORT).show();
               break;
           case R.id.changebutton:
                imageview.setImageResource(R.drawable.eclipse);
               imageview.setScaleType(ImageView.ScaleType.FIT_CENTER);
               break;
           case R.id.imageButton:
               Intent intent = new Intent(MainActivity.this,ButtonActivity.class);
               intent.putExtra("para1",editText.getText().toString());
               startActivity(intent);
               break;
           case R.id.sexbutton:
               if(radiobutton1.isChecked()) editText.setText("男");
               if(radiobutton2.isChecked()) editText.setText("女");
               break;
           case R.id.horrybutton:
               String str=" ";
               if(checkBox1.isChecked()) str+="篮球";
               if(checkBox2.isChecked()) str+="足球";
               if(checkBox3.isChecked()) str+="乒乓球";
               editText.setText(str);
               break;

        }
    }
}
